<?php
/**
 * Created by PhpStorm.
 * User: geyouwen
 * Date: 16/9/6
 * Time: 下午8:41
 */

namespace Home\Controller;


use Think\Controller;
use Think\Page;

class NewsController extends BaseController
{

    private $page_size;
    public function __construct()
    {
        parent::__construct();
        $this->page_size = 20;
    }

    public function index()
    {
        $cate_id = I('get.cate_id', 42, 'intval');

        //分类名
        $cate  = M('article_cate')->where(array('id' => $cate_id))->getField('cate_name');
        $where = array('status' => 1, 'cate_id' => $cate_id);
        $article = M('article'); // 实例化User对象
        // 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
        $count      = $article->where($where)->count();// 查询满足要求的总记录数
        $Page       = new Page($count,$this->page_size);// 实例化分页类 传入总记录数和每页显示的记录数
        $show       = $Page->show();// 分页显示输出
        $list = $article->where($where)->order('addtime desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('cate_name',$cate);// 赋值分页输出
        $this->display(); // 输出模板
    }

    public function notice()
    {
        $where = array('status' => 1, 'cate_id' => 43);
        $article = M('article'); // 实例化User对象
        // 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
        $count      = $article->where($where)->count();// 查询满足要求的总记录数
        $Page       = new Page($count,$this->page_size);// 实例化分页类 传入总记录数和每页显示的记录数
        $show       = $Page->show();// 分页显示输出
        $list = $article->where($where)->order('addtime desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }

    public function detail()
    {
        $id = I('aid');
        $info = M('article')->find($id);
        $content = M('article_content')->where(array('article_id'=>$id))->find();
        $cate_name = M('article_cate')->where('id ='.$info['cate_id'])->getField('cate_name');

        $this->assign('info',$info);
        $this->assign('content',$content);
        $this->assign('cate_name',$cate_name);
        $this->display();
    }

}